package com.sunshine.student_register.ui.activity.register;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.sunshine.student_register.R;
import com.sunshine.student_register.database.student.StudentEntity;
import com.sunshine.student_register.ui.activity.main.MainActivity;

/**
 * 用户注册界面
 */
public class UserRegisterActivity extends AppCompatActivity {

    private UserRegisterViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_register);

        viewModel = new ViewModelProvider(this).get(UserRegisterViewModel.class);
        getSupportActionBar().setTitle("注册学生签到系统");

        initView();
    }

    /**
     * 注册界面基本界面初始化和绑定
     */
    private void initView() {
        Button register = findViewById(R.id.register);

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextInputLayout tilUser = findViewById(R.id.til_user);
                TextInputLayout tilPassword = findViewById(R.id.til_password);
                TextInputLayout tilName = findViewById(R.id.til_name);
                TextInputLayout tilOld = findViewById(R.id.til_old);
                TextInputLayout tilYear = findViewById(R.id.til_year);
                TextInputLayout tilDepartment = findViewById(R.id.til_department);
                TextInputEditText editUser = findViewById(R.id.edit_user);
                TextInputEditText editPassword = findViewById(R.id.edit_password);
                TextInputEditText editName = findViewById(R.id.edit_name);
                TextInputEditText editOld = findViewById(R.id.edit_old);
                TextInputEditText editYear = findViewById(R.id.edit_year);
                TextInputEditText editDepartment = findViewById(R.id.edit_department);
                if (null == editUser.getText() || 0 == editUser.getText().toString().length()){
                    tilUser.setError("请输入正确的用户名");
                }else if (null == editPassword.getText() || 0 == editPassword.getText().toString().length()){
                    tilUser.setErrorEnabled(false);
                    tilPassword.setError("请输入正确的密码");
                }else {
                    tilUser.setErrorEnabled(false);
                    tilPassword.setErrorEnabled(false);

                    if (viewModel.hasThisUser(editUser.getText().toString())){
                        Toast.makeText(UserRegisterActivity.this, "该用户已经存在", Toast.LENGTH_SHORT).show();
                    }else {
                        StudentEntity studentEntity = new StudentEntity(
                                Integer.parseInt(editUser.getText().toString()),
                                editPassword.getText().toString(),
                                editName.getText().toString(),
                                Integer.parseInt(editOld.getText().toString()),
                                Integer.parseInt(editYear.getText().toString()),
                                editDepartment.getText().toString()
                        );
                        viewModel.insertStudent(studentEntity);
                        Toast.makeText(UserRegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(UserRegisterActivity.this, MainActivity.class));
                        finish();
                    }
                }
            }
        });


    }
}
